﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;

using DevExpress.Web.ASPxEditors;

namespace Besture.UI.Admin.Sys
{
    public partial class UserSpecialSet : BasePage
    {
        /// <summary>
        /// 
        /// </summary>
        public string _txtNContentValue = string.Empty;
        /// <summary>
        /// 用户选择角色数组
        /// </summary>
        public List<string> ListRoles = new List<string>();


        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {

            //编辑编号
            InitData(ID);
            //页面加载
            InitPage();
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            rpRoles.DataSource = new RolesDac().GetDataTable();
            rpRoles.DataBind();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id <= 0)
            {
                return;
            }
            AdminUserDac dac = new AdminUserDac();
            AdminUserModel model = dac.GetModel(id);
            txtName.Text = model.LoginName;
            txtName.Enabled = false;
            acbIsFreeze.Checked = Convert.ToBoolean(model.IsFreeze);

            ListRoles = StringHelper.SplitStringToList(model.RolesIds);
        }

        public Boolean HasCheck(string id)
        {
            if (id == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            AdminUserDac dac = new AdminUserDac();
            AdminUserModel model = null;

            var rolesStr = string.Empty;
            //选中角色
            for (int i = 0; i < rpRoles.Items.Count; i++)
            {
                ASPxCheckBox asPxCheckBox = (ASPxCheckBox)rpRoles.Items[i].FindControl("cbRoles");
                if (asPxCheckBox.Checked)
                {
                    Label label = (Label)rpRoles.Items[i].FindControl("lbRolesID");
                    rolesStr += label.Text + ",";
                }
            }


            model = dac.GetModel(ID);
            model.LoginName = txtName.Text.Trim();
            if (!string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                model.Password = txtPassword.Text.Trim();
            }
            model.RolesIds = rolesStr;
            model.IsFreeze = Convert.ToInt32(acbIsFreeze.Checked);
            dac.Update(model);
            FunHelper.Alert("保存成功", "UserList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("UserList.aspx");
        }

        #endregion

        public Boolean HasRoles(string rolesID)
        {
            if (ListRoles.Contains(rolesID))
            {
                return true;
            }
            return false;
        }
    }
}